package com.wt.admin.service.model.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wt.admin.config.prop.IndexProp;
import com.wt.admin.domain.dto.model.ModelListDTO;
import com.wt.admin.domain.entity.model.ModelListEntity;
import com.wt.admin.domain.vo.model.ModelListVO;
import com.wt.admin.mapper.model.ModelListMapper;
import com.wt.admin.service.model.ModelListService;
import com.wt.admin.util.PageUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.io.File;


@Service
public class ModelListServiceImpl extends ServiceImpl<ModelListMapper, ModelListEntity> implements ModelListService {

    @Resource
    private ModelListMapper modelListMapper;
    @Resource
    IndexProp indexProp;

    @Override
    public ModelListEntity addModel(Integer userId,String remark,Object config, Integer type) {
        ModelListEntity entity = new ModelListEntity();
        entity.setModelConfig(config);
        entity.setModelType(type);
        entity.setUserId(userId);
        entity.setRemark(remark);
        save(entity);
        return entity;
    }

    @Override
    public PageUtil.PageVO<ModelListVO> modelList(PageUtil.PageDTO<ModelListDTO> data) {
        return PageUtil.findPage(data, (page) -> modelListMapper.modelList(page, data.getData()));
    }

    @Override
    public ModelListVO modelDel(ModelListDTO data) {
        removeById(data.getId());
        // 根据路径删除模型
        String path = switch(data.getModelType()){
            case 1 -> indexProp.getModelPath().getLangeModel();
            case 2 -> indexProp.getModelPath().getQaModel();
            case 3 -> indexProp.getModelPath().getYoloModel();
            default -> null;
        };
        File file = FileUtil.file(path);
        if(file.exists()){
            FileUtil.del(file);
        }
        return null;
    }

}
